package com.tdf.webblog.handler;

import com.tdf.webblog.api.MySQLRemoteService;
import com.tdf.webblog.entity.AdminMenu;
import com.tdf.webblog.result.Result;
import com.tdf.webblog.result.ResultFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MenuHandler {

    @Autowired
    private MySQLRemoteService mySQLRemoteService;

    @GetMapping("/menu")
    public Result menu() {
        // Get current user in DB.
        String username = SecurityUtils.getSubject().getPrincipal().toString();

        List<AdminMenu> adminMenuList = mySQLRemoteService.getMenusByCurrentUserRemote(username);
        return ResultFactory.buildSuccessResult(adminMenuList);
    }

    @GetMapping("/admin/role/menu")
    public Result listAllMenus() {
        List<AdminMenu> adminMenuList = mySQLRemoteService.getMenusByRoleIdRemote(1);
        return ResultFactory.buildSuccessResult(adminMenuList);
    }
}
